package com.atguigu.eduservice.controller;

import com.atguigu.commonutils.result.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.mysql.cj.x.protobuf.Mysqlx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author kabuda
 * @create 2023-05-06 15:29
 */
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {
    @Autowired
    private EduChapterService eduChapterService;

    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable("courseId") String courseId){
        List<ChapterVo> list=eduChapterService.getChapterVideo(courseId);
        return R.ok().data("list",list);
    }
    @GetMapping("{chapterId}")
    public R getChapterById(@PathVariable("chapterId") String chapterId){
        EduChapter chapter = eduChapterService.getById(chapterId);
        return R.ok().data("chapter",chapter);
    }

    @PostMapping
    public R saveChapter(@RequestBody EduChapter chapter){
        boolean save = eduChapterService.save(chapter);
        if(save){
            return R.ok();
        }
        return R.error();
    }
    @PutMapping
    public R updateChapter(@RequestBody EduChapter chapter){
        boolean save = eduChapterService.updateById(chapter);
        if(save){
            return R.ok();
        }
        return R.error();
    }
    @DeleteMapping("{id}")
    public R delete(@PathVariable("id") String id){
        boolean save = eduChapterService.removeById(id);
        if(save){
            return R.ok();
        }
        return R.error();
    }
}
